﻿using Dal.Properties;
using Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dal
{
    public class TheGoldAventure : IDal
    {
        public List<Location> GetLocations()
        {
            var allLocations = LoadAllLocations();
            var allCommands = LoadAllCommands();

            var results =
                from location in allLocations
                select new Location()
                {
                    Id = location.Id,
                    Title = location.Title,
                    Body = location.Body,
                    Commands = (
                        from c in allCommands
                        where c.LocationId == location.Id
                        select c
                        ).ToList()
                };

            return results.ToList();

        }

        public List<JsonLocation> GetJsonLocations()
        {
            var allLocations = LoadAllJsonLocations();
            var allCommands = LoadAllCommands();

            var results =
                from location in allLocations
                select new JsonLocation()
                {
                    Id = location.Id,
                    Title = location.Title,
                    Body = location.Body,
                    Commands = (
                        from c in allCommands
                        where c.LocationId == location.Id
                        select c
                        ).ToList()
                };

            return results.ToList();

        }

        private List<Location> LoadAllLocations()
        {
            var locsSource = Resources.TheGoldAventureLocations;

            var locsAll = (
            from xLine in locsSource.Split(new char[] { '\n' })
            select xLine.Split('|')
                    );

            var Locs =
            from xRow in locsAll
            where xRow[2] != "^^"
            select new Location
            {
                Id = int.Parse(xRow[0]),
                Title = xRow[2].Replace("^", ""),
                Body = xRow[3].Replace("^", "") + Environment.NewLine
                        + xRow[4].Replace("^", "") + Environment.NewLine
                        + xRow[5].Replace("^", "") + Environment.NewLine
                        + xRow[6].Replace("^", "")

            };
            return Locs.ToList();
        }

        private List<JsonLocation> LoadAllJsonLocations()
        {
            var locsSource = Resources.TheGoldAventureLocations;

            var locsAll = (
            from xLine in locsSource.Split(new char[] { '\n' })
            select xLine.Split('|')
                    );

            var Locs =
            from xRow in locsAll
            where xRow[2] != "^^"
            select new JsonLocation
            {
                Id = int.Parse(xRow[0]),
                Title = xRow[2].Replace("^", ""),
                Body = new string[] {
                    xRow[3].Replace("^", ""),
                        xRow[4].Replace("^", ""),
                        xRow[5].Replace("^", ""),
                        xRow[6].Replace("^", "")
                }
            };
            return Locs.ToList();
        }
        private List<Command> LoadAllCommands()
        {
            var cmdsSource = Resources.TheGoldAventureCommands;

            var cmdsAll = (
                from xLine in cmdsSource.Split(new char[] { '\n' })
                select xLine.Split('|')
                    );

            var commands =
            from xRow in cmdsAll
            select new Command
            {
                Id = Convert.ToInt32(xRow[0]),
                LocationId = Convert.ToInt32(xRow[1]),
                CommandText = xRow[2].Replace("^", ""),
                GoToDestinationId = Convert.ToInt32(xRow[3])
            };

            return commands.ToList();
        }



    }
}
